import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';

import uuid from 'uuid';
// import { systemid } from './config';

interface Message {
	role_name: string;
	role_type_id: string;
	create_time: string;
	icon: string;
	productid: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const c = count(action_id, session_id, systemid);
	c.prepare('pub_role', { role_name: msg.role_name, productid: msg.productid });
	const [t] = await c.exec();
	if (t > 0) {
		return 'other';
	} else {
		const { insert, exec } = operate(action_id, session_id, systemid);
		const role_no = await doc_code(action_id, session_id, systemid, 'BM0011274', '', 1);

		insert('pub_role', { _id: uuid(), role_name: msg.role_name, role_type_id: msg.role_type_id, create_time: msg.create_time, icon: msg.icon, productid: msg.productid, role_no: role_no.result });
		const res = await exec();
		if (res.code === 1) {
			return 'success';
		} else {
			return 'fail';
		}
	}
}
